<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" :busCode="busCode" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		ptFloat,
		minusF,
		errorModal,
		ignoreHtml
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		changeConfigAttr,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','orgId','salaryInstId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		data() {
			return buildFormData({
				postUserCacheKey: "ckd-zb-anytimereport-postUser-cache",
				modelUserId: "",
			})
		},
		watch: {

		},
		methods: {
			onSelectReport(val){
				let _that = this
				_that.$refs.autoform.changeEditor("title1", "modelUserId", {
					link: "../../public/search/search",
					"linkParam": {
						other: JSON.stringify({
							reportTime: val
						}),
						url: 'anyTimeReport/findUserList',
						initload: true,
						cachename: _that.postUserCacheKey,
						namefield:'userPosInstName',
						placeholder: "请输入姓名"
					}
				})
			},
			verifyTitle2(){
				let _that = this
				if(!_that.$refs.autoform.formdata.modelUserId){
					let title = _that.$refs.autoform.getEditorTitle("title1","modelUserId")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			finModeList(id) {
				let _that = this;
				let formdata = _that.$refs.autoform.formdata;
				_that.dataList = [];
				if (formdata.reportTime && id) {
					var param = {
						zuserId: _that.userId,
						reportTime: formdata.reportTime,
						userId: id,
						dataStr: '[]',
					};
					confirmModal(
						_that,
						'提示信息',
						'是否赋值承担人所有未填报的计划项指标？',
						function() {
							_that.om.request('anyTimeReport/findModeList', param, 'post').then(res => {
								if (res.succeed) {
									_that.subEditorObj["title2"].workList = res.value;
								} else {
									_that.om.toast(res.msg);
								}
							});
						},
						function() {
							return false;
						}
					);
				} else {
					_that.$refs.autoform.clear('title1', 'modelUserId')
					let title = _that.$refs.autoform.getEditorTitle("topinfo", "reportTime")
					_that.om.toast("请先选择" + title, 3000)
				}
			},
		verifyModelUser() {
			let _that = this
			if (!_that.$refs.autoform.formdata.reportTime) {
				let title = _that.$refs.autoform.getEditorTitle("topinfo", "reportTime")
				_that.om.toast("请先选择" + title)
				return false
			} else {
				return true
			}
		},
		clearModelUser(){
			let _that = this
			_that.$refs.autoform.clear("title1","modelUserId")
			_that.$refs.autoform.clear("title1","modelPostId")
			_that.$refs.autoform.clear("title1","modelInstId")
			_that.subEditorObj["title2"].workList = []
		},
		verifyInfo() {
			let formdata = this.$refs.autoform.formdata
			if (!formdata.reportTime || !formdata.modelUserId) {
				this.om.toast('请先选择统计日期和姓名', 3000)
				return false
			}
		},
		formSubmit() {
			let _that = this
			let arr = []
			_that.formdata.orgId = _that.orgId
			_that.formdata.zinstId = _that.salaryInstId
			for(let index in _that.subEditorObj["title2"].workList){
				if(!_that.subEditorObj["title2"].workList[index].relationList){
					_that.subEditorObj["title2"].workList[index].relationList = []
				}
				if(_that.subEditorObj["title2"].workList[index].sum != 0 && !_that.subEditorObj["title2"].workList[index].sum){
					arr.push(Number(index)+1)
				}
			}
			if(arr.length){
				confirmModal(
					_that,
					'提示信息',
					"填报指标信息，第"+arr+"行本人执行数为空，在审批后将做 0 处理，是否继续保存？",
					function() {
						if (_that.edit) {
							_that.$set(_that.formdata,"dataStr",JSON.stringify(_that.subEditorObj["title2"].workList))
							_that.om.formSave('anyTimeReport/doUpdate', _that.formdata, _that)
						} else {
							_that.om.formSave('anyTimeReport/doAdd', _that.formdata, _that)
						}
					},
					function() {
					}
				);
			}else{
				if (_that.edit) {
					_that.om.formSave('anyTimeReport/doUpdate', _that.formdata, _that)
				} else {
					_that.om.formSave('anyTimeReport/doAdd', _that.formdata, _that)
				}
			}
		},
		getWorkParam() {
			let _that = this
				return {
					reportTime: _that.$refs.autoform.formdata.reportTime,
					userId: _that.$refs.autoform.formdata.modelUserId,
				}
		},
		initLoad(options) {
			let _that = this
			let ajaxArr = []
			var fileId = options.fileId
			ajaxArr.push({
				url: "wxPosBusiAuth/isnewList",
				param: {
					code: "HRS1204"
				},
				method: "post"
			})
			if (fileId) {
				ajaxArr.push({
					url: "anyTimeReport/getValue",
					param: {
						fileId: fileId
					}
				})
			}
			formModelLoad(ajaxArr, function(res, mainData) {
				changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posName'),mainData)
					if (_that.subEditorObj["title2"]) {
						changeEditor('title2', 'dataStr', {
							getWorkParam: "getWorkParam",
							titleTemplate: "<%= indexDictName %>"
						}, null, mainData)
					}
					//子表
					if (fileId) {
						var fieldArr = getFormFields(mainData)
						fieldArr.push("id", "modelUserId", "modelPostId", "userName", "userId")
						var dataList = _that._.cloneDeep(res[1].value.dataList)
						if (dataList.length) {
							dataList.forEach(e => delete e.children)
						}
						_that.subEditorObj["title2"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "dataStr", JSON.stringify(
							dataList))
						_that.$set(_that.formdata, "dataStr", JSON.stringify(dataList))
						delete res[1].value.dataList
						for (var i in res[1].value) {
							if (fieldArr.includes(i)) {
								if (i === "postUserName") {
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, res[1].value[
										"modelUserName"] + "/" + res[1].value["modelPostName"])
									_that.$set(_that.formdata, i, res[1].value["modelUserName"] + "/" + res[1]
										.value["modelPostName"])
								} else {
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, res[1].value[i])
									_that.$set(_that.formdata, i, res[1].value[i])
								}
							}
						}
						_that.$nextTick(function() {
								_that.$refs.autoform.changeEditor("topinfo","reportTime","type","labelbox")
								_that.$refs.autoform.changeEditor("title1","modelUserId","type","labelbox")
								_that.$refs.autoform.changeEditor("title1","modelPostId","type","labelbox")
							if(this.$refs.autoform){
								this.$refs.autoform.initSelectIndex('topinfo', 'zposId')
							}
						})
					}
				
				_that.$nextTick(function() {
					if(hasEditor('topinfo',"zposId",mainData)){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
					}
				})
			}, options)
		}
	},
	onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key: _that.postUserCacheKey,
					success(res) {
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.modelUserId = d.id
						formdata.modelUserName = d.username
						_that.$refs.autoform.clear("title1","modelPostId")
						_that.$refs.autoform.clear("title1","modelInstId")
						_that.subEditorObj["title2"].workList = []
						if (_that.userId == d.id) {
							confirmModal(_that, '提示信息', '不能选择与制表人相同人员，请重新选择', () => {
								_that.$refs.autoform.clear("title1","modelUserId")
							})
						}else{
							formdata.modelPostId = d.salaryPosId
							formdata.modelPostName = d.salaryPosName
							formdata.modelInstId = d.salaryPosInstId
							formdata.modelInstName = d.salaryPosInstName
							_that.finModeList(d.id)
						}
						uni.removeStorage({
							key: _that.postUserCacheKey
						})
					}
				})
			}

		},
		onLoad(options) {
			this.initLoad(options)
		}
	}
</script>

<style>
</style>
